package com.qyy.www_chatroom.config;

import com.qyy.www_chatroom.component.WebSocketAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

@Configuration
@EnableWebSocket //启动websocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Autowired
    private WebSocketAPI webSocketAPI;
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        //通过这个方法。把刚才创建好的Handler类注册到具体的路径上

        registry.addHandler(webSocketAPI,"/WebSocketMessage")
                //通过这个拦截器就可以把用户给HttpSession中添加的Attribute在WebSocketSession也被加入一份
                .addInterceptors(new HttpSessionHandshakeInterceptor());

    }
}
